package youleche.com.controler.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import youleche.com.common.MeilecheChinaAreaCrawler;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.factory.MLLogFactory;

/**
 * Servlet implementation class SAreaServlet
 */
public class SAreaServlet extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SAreaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0:
				//抓数据
				this.crawAreaData(request, response);
				break;
		}
	}

	/**
	 * 抓数据
	 * @param request
	 * @param response
	 */
	private void crawAreaData(HttpServletRequest request, HttpServletResponse response){
		MeilecheChinaAreaCrawler handler = new MeilecheChinaAreaCrawler();
		String year = "2013";
		try {
			handler.crawlChinaArea(year);
			super.response(response, "抓取数据成功");
		} catch (Exception e) {
			try {
				handler.crawlChinaArea(year);
			} catch (Exception e1) {
				MLLogFactory.exceptionLog("二次抓取数据出错", "二次抓取数据出错", e1.getMessage(), request.getRemoteAddr());				
			}
			MLLogFactory.exceptionLog("抓取数据出错", "抓取数据出错", e.getMessage(), request.getRemoteAddr());
			
		}
	}
}
